/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package it.unitn.science.matr155863_148346_145963.bccstore.servlet;

import it.unitn.science.matr155863_148346_145963.bccstore.DBManager;
import it.unitn.science.matr155863_148346_145963.bccstore.items.Commission;
import it.unitn.science.matr155863_148346_145963.bccstore.items.User;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.UnavailableException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;

/**
 *
 * @author berto
 */
public class ShowBillProductSoldSRV extends HttpServlet {

    private DBManager db;
    private static Logger log = Logger.getLogger(ShowBillProductSoldSRV.class);
    
   
    @Override
    public void init() throws UnavailableException{
            db=(DBManager)super.getServletContext().getAttribute("db_manager");
    }    
    
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        
        Integer id=((User)request.getSession().getAttribute("user")).getId();
        try {
            
            ArrayList<Commission> commissions = new ArrayList<Commission>();
            out.println("<div class=\"content\" >");
            commissions = db.getAllSellCommission(id);
            if(commissions.isEmpty()){
                out.println("<h2>Nobody has buy your product</h2>");
            }else{
                out.println("<h2>Your product sell</h2>");
                out.println("<table>");
                out.println("<tr>");
                out.println("<td>Nome Prodotto</td>");
                out.println("<td>prezzo</td>");
                out.println("<td>quantita</td>");
                out.println("<td>unita misura</td>");
                out.println("<td>Username Compratore</td>");
                out.println("<td>FirstName Compratore</td>");
                out.println("<td>LastName Compratore</td>");
                out.println("<td>Link of bill</td>");
                out.println("</tr>");
                Iterator<Commission> it = commissions.iterator(); 
                while(it.hasNext()){ 
                    Commission c = it.next() ;
                    out.println("<tr>");
                    out.println("<td>"
                                + "<a href=\""+response.encodeURL(request.getContextPath()+"/contents/user/ShowProductDetails?id="
                                    +c.getProductId())+"\">"
                                    +c.getProduct().getName()
                                +"</a>"
                            + "</td>");
                    out.println("<td>"+c.getPrice()+"</td>");
                    out.println("<td>"+c.getQuantity()+"</td>");
                    out.println("<td>"+c.getUmName()+"</td>");
                    out.println("<td>"+c.getBuyerUserName()+"</td>");
                    out.println("<td>"+c.getBuyerFirstName()+"</td>");
                    out.println("<td>"+c.getBuyerLastName()+"</td>");
                    out.println("<td>"
                                + "<a href=\""+response.encodeURL(request.getContextPath()+"/bill/"+ c.getBill())+"\""
                                    +">download bill</a>"
                                + "</td>");
                    out.println("</tr>");
                    out.println("</tr>");
                }
                out.println("</table>");
            }   
            out.println("</div>");
         }catch(Exception ex){
            log.error(ex);
            RequestDispatcher dispatcher=request.getRequestDispatcher("/contents/public/errorPage.html");
            dispatcher.include(request, response);
        } finally {    
           // out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
